package c.a.a;

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import c.a.a.d0.h0.d;
import c.a.a.d0.s;
import g.q;
import g.u;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/* compiled from: LottieCompositionFactory */
public class g {
    public static final Map<String, t<f>> a = new HashMap();

    /* compiled from: LottieCompositionFactory */
    public class c implements Callable<r<f>> {
        public final /* synthetic */ f a;

        public c(f fVar) {
            this.a = fVar;
        }

        public Object call() throws Exception {
            return new r(this.a);
        }
    }

    /* compiled from: LottieCompositionFactory */
    public class a implements n<f> {
        public final /* synthetic */ String a;

        public a(String str) {
            this.a = str;
        }

        public void onResult(Object obj) {
            f fVar = (f) obj;
            g.a.remove(this.a);
        }
    }

    /* compiled from: LottieCompositionFactory */
    public class b implements n<Throwable> {
        public final /* synthetic */ String a;

        public b(String str) {
            this.a = str;
        }

        public void onResult(Object obj) {
            Throwable th = (Throwable) obj;
            g.a.remove(this.a);
        }
    }

    public static t<f> a(String str, Callable<r<f>> callable) {
        f fVar;
        if (str == null) {
            fVar = null;
        } else {
            c.a.a.b0.g gVar = c.a.a.b0.g.b;
            Objects.requireNonNull(gVar);
            fVar = (f) gVar.a.a(str);
        }
        if (fVar != null) {
            return new t(new c(fVar), false);
        }
        if (str != null) {
            Map map = a;
            if (map.containsKey(str)) {
                return (t) map.get(str);
            }
        }
        t tVar = new t(callable, false);
        if (str != null) {
            tVar.b(new a(str));
            tVar.a(new b(str));
            a.put(str, tVar);
        }
        return tVar;
    }

    public static t<f> b(Context context, String str) {
        String D = c.b.a.a.a.D("asset_", str);
        return a(D, new i(context.getApplicationContext(), str, D));
    }

    public static r<f> c(Context context, String str, String str2) {
        try {
            if (str.endsWith(".zip")) {
                return g(new ZipInputStream(context.getAssets().open(str)), str2);
            }
            return d(context.getAssets().open(str), str2);
        } catch (IOException e) {
            return new r(e);
        }
    }

    public static r<f> d(InputStream inputStream, String str) {
        try {
            u uVar = new u(q.c(inputStream));
            String[] strArr = c.a.a.d0.h0.c.e;
            r<f> e = e(new d(uVar), str, true);
            return e;
        } finally {
            c.a.a.e0.g.b(inputStream);
        }
    }

    public static r<f> e(c.a.a.d0.h0.c cVar, String str, boolean z) {
        try {
            Object a = s.a(cVar);
            if (str != null) {
                c.a.a.b0.g gVar = c.a.a.b0.g.b;
                Objects.requireNonNull(gVar);
                gVar.a.b(str, a);
            }
            r rVar = new r(a);
            if (z) {
                c.a.a.e0.g.b(cVar);
            }
            return rVar;
        } catch (Exception e) {
            r rVar2 = new r(e);
            if (z) {
                c.a.a.e0.g.b(cVar);
            }
            return rVar2;
        } catch (Throwable e2) {
            if (z) {
                c.a.a.e0.g.b(cVar);
            }
            throw e2;
        }
    }

    public static r<f> f(Context context, int i, String str) {
        try {
            return d(context.getResources().openRawResource(i), str);
        } catch (NotFoundException e) {
            return new r(e);
        }
    }

    public static r<f> g(ZipInputStream zipInputStream, String str) {
        try {
            r<f> h = h(zipInputStream, str);
            return h;
        } finally {
            c.a.a.e0.g.b(zipInputStream);
        }
    }

    public static r<f> h(ZipInputStream zipInputStream, String str) {
        HashMap hashMap = new HashMap();
        try {
            ZipEntry nextEntry = zipInputStream.getNextEntry();
            Object obj = null;
            while (nextEntry != null) {
                String name = nextEntry.getName();
                String[] strArr;
                if (name.contains("__MACOSX")) {
                    zipInputStream.closeEntry();
                } else if (nextEntry.getName().contains(".json")) {
                    u uVar = new u(q.c(zipInputStream));
                    strArr = c.a.a.d0.h0.c.e;
                    obj = (f) e(new d(uVar), null, false).a;
                } else {
                    if (!name.contains(".png")) {
                        if (!name.contains(".webp")) {
                            zipInputStream.closeEntry();
                        }
                    }
                    strArr = name.split("/");
                    hashMap.put(strArr[strArr.length - 1], BitmapFactory.decodeStream(zipInputStream));
                }
                nextEntry = zipInputStream.getNextEntry();
            }
            if (obj == null) {
                return new r(new IllegalArgumentException("Unable to parse composition"));
            }
            for (Entry entry : hashMap.entrySet()) {
                String str2 = (String) entry.getKey();
                for (m mVar : obj.d.values()) {
                    if (mVar.d.equals(str2)) {
                        break;
                    }
                }
                m mVar2 = null;
                if (mVar2 != null) {
                    mVar2.e = c.a.a.e0.g.e((Bitmap) entry.getValue(), mVar2.a, mVar2.b);
                }
            }
            for (Entry entry2 : obj.d.entrySet()) {
                if (((m) entry2.getValue()).e == null) {
                    StringBuilder g = c.b.a.a.a.g("There is no image for ");
                    g.append(((m) entry2.getValue()).d);
                    return new r(new IllegalStateException(g.toString()));
                }
            }
            if (str != null) {
                c.a.a.b0.g gVar = c.a.a.b0.g.b;
                Objects.requireNonNull(gVar);
                gVar.a.b(str, obj);
            }
            return new r(obj);
        } catch (IOException e) {
            return new r(e);
        }
    }

    public static String i(Context context, int i) {
        StringBuilder g = c.b.a.a.a.g("rawRes");
        g.append(((context.getResources().getConfiguration().uiMode & 48) == 32 ? 1 : null) != null ? "_night_" : "_day_");
        g.append(i);
        return g.toString();
    }
}
